package net.shambolica.ember.jinterface.wrappers.objects;

import com.ericsson.otp.erlang.OtpErlangAtom;
import com.ericsson.otp.erlang.OtpErlangBinary;
import com.ericsson.otp.erlang.OtpErlangDouble;
import com.ericsson.otp.erlang.OtpErlangFloat;
import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangLong;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangRef;
import com.ericsson.otp.erlang.OtpErlangTuple;
import com.ericsson.otp.erlang.OtpLocalNode;
import static net.shambolica.ember.utils.StringUtils.isEmpty;

/**
 * Factory for generating ErlangOtpObjects.
 * Some methods might reference a Node (e.g. for generation of refs).
 */
public class EOT {

    public static OtpErlangAtom atom(String value) {
        if (isEmpty(value)) {
            throw new IllegalArgumentException("empty string value");
        }
        return new OtpErlangAtom(value);
    }


    public static OtpErlangAtom atom(boolean value) {
        return new OtpErlangAtom(value ? "true" : "false");
    }


    public static OtpErlangBinary binary(byte[] bytes) {
        if (bytes == null) {
            bytes = new byte[0];
        }
        return new OtpErlangBinary(bytes);
    }


    public static OtpErlangLong number(int n) {
        return new OtpErlangLong((long)n);
    }


    public static OtpErlangLong number(long n) {
        return new OtpErlangLong(n);
    }


    public static OtpErlangFloat number(float n) {
        return new OtpErlangFloat(n);
    }


    public static OtpErlangDouble number(double n) {
        return new OtpErlangDouble(n);
    }


    public static OtpErlangRef ref(OtpLocalNode node) {
        return node.createRef();
    }


    public static OtpErlangList list(String latin1) {
        return new OtpErlangList(latin1);
    }


    public static OtpErlangList list() {
        return new OtpErlangList();
    }


    public static OtpErlangList list(OtpErlangObject ... objects) {
        return new OtpErlangList(objects);
    }


    public static OtpErlangTuple tuple(OtpErlangObject ... objects) {
        return new OtpErlangTuple(objects);
    }

}
